package org.hpm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hpm.bo.HpmUserBO;
import org.hpm.common.api.vo.HpmResult;
import org.hpm.common.api.vo.Result;
import org.hpm.common.aspect.annotation.AutoLog;
import org.hpm.common.system.base.controller.HpmBaseController;
import org.hpm.common.system.query.QueryGenerator;
import org.hpm.entity.HpmUsers;
import org.hpm.service.IHpmUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @description: 用户表
 * @author: jacklin
 * @date: 2021-11-24
 * @version: V1.0
 */
@Api(tags = "用户表")
@RestController
@RequestMapping("/hpmUsers")
@Slf4j
public class HpmUsersController extends HpmBaseController<HpmUsers, IHpmUsersService> {

    @Autowired
    private IHpmUsersService hpmUsersService;

    /**
     * 判断用户名是否存在
     *
     * @author: jacklin
     * @since 2022/6/21 22:08
     **/
    @ApiOperation(value = "判断用户名是否存在", notes = "判断用户名是否存在", httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public HpmResult usernameIsExist(@RequestParam String username) {
        boolean isExist = hpmUsersService.usernameIsExist(username);
        if (isExist) {
            return HpmResult.errorMsg("用户名已存在！");
        } else {
            return HpmResult.ok("该值可用！");
        }
    }

    /**
     * 注册
     *
     * @author: jacklin
     * @since 2022/6/21 22:32
     **/
    @PostMapping(value = "/register")
    public HpmResult register(@RequestBody HpmUserBO hpmUserBO, HttpServletRequest request, HttpServletResponse response) {
        return hpmUsersService.createUser(hpmUserBO, request, response);
    }

    /**
     * 登录
     *
     * @author: jacklin
     * @since 2022/6/21 22:32
     **/
    @PostMapping(value = "/login")
    public HpmResult login(@RequestBody HpmUserBO hpmUserBO, HttpServletRequest request, HttpServletResponse response) {
        return hpmUsersService.login(hpmUserBO, request, response);
    }

    /**
     * 退出登录
     *
     * @author: jacklin
     * @since 2022/8/31 11:33
     **/
    @PostMapping(value = "/logout")
    public HpmResult logout(@RequestParam String userId, HttpServletRequest request, HttpServletResponse response) {
        return hpmUsersService.logout(userId, request, response);
    }


    /**
     * 分页列表查询
     *
     * @param hpmUsers
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "用户表-分页列表查询")
    @ApiOperation(value = "用户表-分页列表查询", notes = "用户表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HpmUsers hpmUsers,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<HpmUsers> queryWrapper = QueryGenerator.initQueryWrapper(hpmUsers, req.getParameterMap());
        Page<HpmUsers> page = new Page<HpmUsers>(pageNo, pageSize);
        IPage<HpmUsers> pageList = hpmUsersService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hpmUsers
     * @return
     */
    @AutoLog(value = "用户表-添加")
    @ApiOperation(value = "用户表-添加", notes = "用户表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HpmUsers hpmUsers) {
        hpmUsersService.save(hpmUsers);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hpmUsers
     * @return
     */
    @AutoLog(value = "用户表-编辑")
    @ApiOperation(value = "用户表-编辑", notes = "用户表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HpmUsers hpmUsers) {
        hpmUsersService.updateById(hpmUsers);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户表-通过id删除")
    @ApiOperation(value = "用户表-通过id删除", notes = "用户表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hpmUsersService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "用户表-批量删除")
    @ApiOperation(value = "用户表-批量删除", notes = "用户表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hpmUsersService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户表-通过id查询")
    @ApiOperation(value = "用户表-通过id查询", notes = "用户表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        HpmUsers hpmUsers = hpmUsersService.getById(id);
        if (hpmUsers == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hpmUsers);
    }
}
